package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepsitory;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerRepsitory bannerRepsitory;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;


    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(Banner banner) {
        banner.setId(null);
        bannerRepsitory.save(banner);
    }

    @Override
    public void update(Banner banner) {
        bannerRepsitory.save(banner);
    }

    @Override
    public void delete(String id) {
        bannerRepsitory.deleteById(id);
    }

    @Override
    public Banner get(String id) {
        return bannerRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<Banner> list() {
        return bannerRepsitory.findAll();
    }

    //页面数据
    @Override
    public Page<Banner> query(BannerQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");

        return DBHelper.query(query, Banner.class, template, pageable);
    }

    //保存或修改
    @Override
    public void saveOrUpdate(Banner banner) {
        if (!StringUtils.hasLength(banner.getId())) {
            this.save(banner);
        }
        this.update(banner);
    }


    //获取攻略和游记
    @Override
    public Map getAllType() {
        HashMap<String, Object> map = new HashMap<>();
        //攻略
        List<Strategy> sts = strategyService.list();
        //游记
        List<Travel> ts = travelService.list();
        map.put("sts", sts);
        map.put("ts", ts);
        return map;
    }

    //首页推荐游记前5个
    @Override
    public List<Banner> getTop5(int type) {
        Pageable pageable = PageRequest.of(0, 5,
                Sort.Direction.DESC, "sequence");
        List<Banner> list = bannerRepsitory.findByTypeAndState(type,
                Banner.STATE_NORMAL, pageable);
        return list;
    }

    //首页推荐攻略
    @Override
    public List<Banner> getTravel(int typeStrategy) {
        Pageable pageable = PageRequest.of(0, 3,
                Sort.Direction.DESC, "sequence");
        List<Banner> list = bannerRepsitory.findByTypeAndState(typeStrategy,
                Banner.STATE_NORMAL, pageable);
        return list;
    }
}
